package com.cs.framework.jdbc.mybatis3.builder;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * 构建配置信息
 * 
 * @author hedongzhou
 * 
 */
public class ConfigBuilder {

	/**
	 * 数据源
	 */
	private DataSource dataSource;

	/**
	 * 配置信息
	 */
	private Configuration configuration;

	public ConfigBuilder(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 创建配置
	 * 
	 * @param id
	 * @return
	 */
	public Configuration build(String id) {
		TransactionFactory txFactory = new JdbcTransactionFactory();
		Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory).dataSource(
				dataSource);

		Configuration configuration = new Configuration();
		configuration.setEnvironment(environmentBuilder.build());

		return configuration;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

}
